package classroom.edu.info.manager.dao;

import classroom.edu.info.manager.domain.Student;

public class StudentDao {
    private Student[] student = new Student[5];

    public boolean addStudent(Student stu) {

        int index = -1;
        for (int i = 0; i < student.length; i++) {
            if (student[i] == null) {
                index = i;
                break;
            }
        }
        if (index != -1) {
            student[index] = stu;
            return true;
        }
        return false;
    }

    public Student[] selectStudent() {
        return student;
    }

    public void deleteStudent(String id) {
        int index = getIndex(id);
        if(index != -1) {
            student[index] = null;
        }
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < student.length; i++) {
            if (student[i].getId().equals(id)){
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateStudent(Student newStudent) {
        int index = getIndex(newStudent.getId());
        student[index] = newStudent;

    }
}
